<template>
  <EnteringForm
    ref="enteringForm"
    v-bind="dialog"
		width="40%"
    @close="close"
    @submit="submit"
  >
    <el-form ref="formRef" :model="queryParams" :rules="rules" label-width="120px">
      <el-row type="flex" style="flex-wrap: wrap">
				<el-col :span="24" v-if="dialogType!='add'">
          <el-form-item label="编号:" prop="id">
            <!-- <el-input
              v-model.trim="queryParams.id"
              placeholder="请输入"
              clearable
            ></el-input> -->
						<span>{{ queryParams.id }}</span>
          </el-form-item>
        </el-col>
				<el-col :span="24">
          <el-form-item label="优惠券名称:" prop="name">
            <el-input
              v-model.trim="queryParams.name"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
				<el-col :span="24">
					<el-form-item label="商家" prop="storeId">
						<el-select
							v-model="queryParams.storeId"
							placeholder="请选择"
							style="width: 100%"
							clearable
						>
							<el-option :label="item.name" :value="item.id" v-for="item in shopList" :key="item.id"/>
						</el-select>
					</el-form-item>
				</el-col>
        <el-col :span="24">
					<el-form-item label="面值:" prop="faceValue">
						<el-input-number
							style="width:100%"
							controls-position="right" 
							:precision="2"
							:min="0"
							:max="99999999"
							v-model.trim="queryParams.faceValue"
							placeholder="请输入"
							clearable
						/>
					</el-form-item>
				</el-col>
        <el-col :span="24">
					<el-form-item label="数量:" prop="publishNum">
						<el-input-number
							style="width:100%"
							controls-position="right" 
							:precision="0"
							:min="0"
							:max="99999999"
							v-model.trim="queryParams.publishNum"
							placeholder="请输入"
							clearable
						/>
					</el-form-item>
				</el-col>
        <el-col :span="24">
					<el-form-item label="满减限制:" prop="threshold">
						<el-input-number
							style="width:100%"
							controls-position="right"
							:precision="2"
							:min="0"
							:max="99999999"
							v-model.trim="queryParams.threshold"
							placeholder="请输入满减限制（使用门槛）"
							clearable
						/>
					</el-form-item>
				</el-col>
				
        <el-col :span="24">
					<el-form-item label="有效期:" prop="expireDay">
						<el-date-picker
							v-model="queryParams.expireDay"
							style="width: 100%;"
							type="date"
							value-format="YYYY-MM-DD"
							:disabledDate="pickerOptions"
							placeholder="选择失效日期">
						</el-date-picker>
					</el-form-item>
				</el-col>
        <!-- <el-col :span="24">
					<el-form-item label="赠送积分:" prop="integral">
						<el-input-number
							style="width:100%"
							controls-position="right" 
							:precision="0"
							:min="0"
							:max="99999999"
							v-model.trim="queryParams.integral"
							placeholder="请输入"
							clearable
						/>
					</el-form-item>
				</el-col> -->
      </el-row>
    </el-form>
  </EnteringForm>
</template>

<script setup>
import { getCurrentInstance, reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { listPage, info, add, edit, del, changeStatusApi } from '@/api/operates/coupon.js'

const { proxy } = getCurrentInstance()
const emit = defineEmits(["success"]);
const prop = defineProps({
	shopList: {
    type: Array,
    default: ()=>[],
  },
})
const dialogType = ref('')
const dialog = reactive({
  visible: false,
  title: "",
});
const pickerOptions = (v) => {
			// console.log('vvv', v)
			return v.getTime() <= new Date().getTime() - 86400000;//  - 86400000是否包括当天
		}
const rules = reactive({
  // sellerId: [{ required: true, message: "请选择", trigger: "change" }],
  name: [{ required: true, message: "请输入", trigger: "blur" }],
  faceValue: [{ required: true, message: "请输入", trigger: "blur" }],
  publishNum: [{ required: true, message: "请输入", trigger: "blur" }],
  threshold: [{ required: true, message: "请输入", trigger: "blur" }],
  expireDay: [{ required: true, message: "请输入", trigger: "blur" }],
	
  sort: [{ required: true, message: "请输入", trigger: "blur" }],
  integral: [{ required: true, message: "请输入", trigger: "blur" }],
  cover: [{ required: true, message: "请上传", trigger: "blur" }],
  images: [{ required: true, message: "请上传", trigger: "blur" }],
  intro: [{ required: true, message: "请上传", trigger: "blur" }],
  enable: [{ required: true, message: "请选择", trigger: "change" }],
  recommend: [{ required: true, message: "请选择", trigger: "change" }],
  storeId: [{ required: true, message: "请选择", trigger: "change" }],


});
const queryParams = ref({
	enable: 1,
	recommend: 0,
})

function init(type, record){
	dialogType.value = type
	dialogType.value=='add' ? dialog.title='新增' : dialog.title='编辑'
	dialog.visible = true
	// 如果是编辑，回显数据
	if(dialogType.value=='edit'){
		queryParams.value = { ...record };
	}
};

function close(){
  dialog.visible = false;
  proxy.$refs.formRef.resetFields();
	queryParams.value = {}
};
function submit() {
  proxy.$refs.formRef.validate((valid) => {
    if (valid) {
      let query = {
        ...queryParams.value,
				limitNum: 1,
        // cover:'test',
        // intro:'test',
        // images:'test'
      }
      if(dialogType.value=='add'){
				add(query).then((res) => {
					if (res.code == 200) {
						ElMessage.success("新增成功");
						close();
						emit("success",'add');
					} else {
						ElMessage.error(res.message || "新增失败");
					}
				});
			} else {
				edit(query).then((res) => {
					if (res.code == 200) {
						ElMessage.success("编辑成功");
						close();
						emit("success",'edit');
					} else {
						ElMessage.error(res.message || "编辑失败");
					}
				});
			}
    }
  });
}
defineExpose({ init });
</script>

<style scoped>
.v-enter-active,
.v-leave-active {
  transition: opacity 0.5s ease;
}

.v-enter-from,
.v-leave-to {
  opacity: 0;
}
:deep(.el-input__wrapper){
	width: 100%;
}
</style>
